#include "matrixwidget.h"
#include "ui_matrixwidget.h"
#include <QChar>
#include <QMessageBox>
#include <QStringList>
#include <QString>
#include <QThreadPool>
#include <string>
#include "./traversethread.h"
#include <QTableWidgetItem>
#include <QLabel>
MatrixWidget::MatrixWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MatrixWidget)
{
    ui->setupUi(this);
    mg=new MGraph<string>(UDN);

    (ui->panel)->SetALGraph(mg);
    mg->InsertVex("v1");
    mg->InsertVex("v2");
    mg->InsertVex("v3");
    mg->InsertVex("v4");
    mg->InsertVex("v5");
    mg->InsertVex("v6");
    mg->InsertVex("v7");
    mg->InsertVex("v8");
    mg->InsertArc("v1","v2");
    mg->InsertArc("v1","v3");
    mg->InsertArc("v2","v5");
    mg->InsertArc("v2","v4");
    mg->InsertArc("v4","v8");
    mg->InsertArc("v5","v8");
    mg->InsertArc("v3","v6");
    mg->InsertArc("v6","v7");
    mg->InsertArc("v3","v7");
    tt=new TraverseThread(this,ui->panel,mg);
    //new TraverseThread()
    qDebug()<<"vexnum==="<<mg->vexnum;
    (ui->panel)->setPolygon(2, 250, ui->panel->size().width(),ui->panel->height()); // Example: 7-sided polygon with radius 100 centered at (150, 150)

    update();

}

MatrixWidget::~MatrixWidget()
{
    delete ui;
}

void MatrixWidget::on_pushButton_clicked()
{
    string c;
    if(!(c=ui->lineEdit->text().toStdString()).empty()){
        if(mg->repeat(c)){
            QMessageBox::information(this,"提示","重复了");
            return;
        }
        mg->InsertVex(c);
    }else{
        QMessageBox::information(this,"提示","输入有误");
    }
    update();

}
void MatrixWidget::paintEvent(QPaintEvent *event){
    ui->arcstable->setRowCount(mg->GetVexCount());
    ui->arcstable->setColumnCount(mg->GetVexCount());

    QStringList verslist;
    mg->TraverseVex([&](string data){
          verslist<<QString::fromStdString(data);
    });
    ui->arcstable->setHorizontalHeaderLabels(verslist);
    ui->arcstable->setVerticalHeaderLabels(verslist);
    mg->TraverseArc([&](int from,int to,MGraph<string>::ArcCell &cell){
        ui->arcstable->setCellWidget(from,to,new QLineEdit(QString::number(cell.adj)+(cell.info!=nullptr?QString::fromStdString(string(cell.info)):"")));
    });
    //ui->arcstable->cellWidget()
}


void MatrixWidget::on_addArcBtn_clicked()
{
    string fromstr=ui->fromvex->text().toStdString();
    string tostr=ui->tovex->text().toStdString();
    string msg;
    const char *str;
    if(!(msg=ui->addmsg->text().toStdString()).empty()){

        str=(new string(msg))->c_str();
    }else{
        str=nullptr;
    }
    if(!(fromstr.empty()||tostr.empty())){
        if(!mg->InsertArc(fromstr,tostr,str)){
            QMessageBox::critical(this,"提示","输入有误");
        }else{
             QMessageBox::information(this,"成功","插入成功");
        }

    }else{
        QMessageBox::information(this,"提示","输入有误");
    }
    update();
}


void MatrixWidget::on_delArcBtn_clicked()
{
    string fromstr=ui->fromvex->text().toStdString();
    string tostr=ui->tovex->text().toStdString();
    if(!(fromstr.empty()||tostr.empty())){
        // if(!mg->DeleteArc(fromstr,tostr)){
        //     QMessageBox::critical(this,"提示","输入有误");
        // }else{
        //     QMessageBox::information(this,"成功","删除成功");
        // }

    }else{
        QMessageBox::information(this,"提示","输入有误");
    }
    update();
}


void MatrixWidget::on_pushButton_4_clicked()
{
    tt->SwitchDFS();
    QThreadPool::globalInstance()->start(tt);
}


void MatrixWidget::on_pushButton_2_clicked()
{
    tt->SWitchBFS();
    QThreadPool::globalInstance()->start(tt);
}

